package com.yjhh.scala

import java.io.File

/**
  * 1.当我们使用一个类型的使用如果当前类型无法直接找到合适的方法，则会到其伴生对象中查找隐士功能
  * 2.当前类的伴生对象无法找到相关功能，则会到该类相关的类和接口的伴生对象中去查找
  * 3.作用域明确的导入比占位符导入优先级高，当前作用域变量级别最高
  *   val content = "I love spark"
  *   import ImplicitsMsg.content  高
  *   import Implicits._     低
  * 4.implicitly方法可以获取参数实际运行时具体的数值
  */
class Implicits(x : Int){
  def printValue = {
    val x = 100
    println(x)
  }
}

class RicherFile(val file:File){
  def dtspark(): Unit ={
    println("DTSpark")

    val count = sc.parallelize(1 to 10).filter { _ =>
      val x = math.random
      val y = math.random
      x*x + y*y < 1
    }.count()
    println(s"Pi is roughly ${4.0 * count / 10}")
  }
}

object Implicits{
  implicit val content = "Scala Spark"
  implicit def file2RicherFile(file:File) = new RicherFile(file)
  implicit def richerFile2File(richerFile:RicherFile) = richerFile.file
  implicit def int2String(x : Int) = x.toString
}
object ImplicitsMsg{
  implicit val content = "Hadoop"
}

object ImplicitsAdvanced extends App {

  def printContent(implicit content:String) = println(content)
  val content = "I love spark"
  import ImplicitsMsg.content
  import Implicits._
  printContent(content)
  printContent(100)

  val file = new File(".")
  file.dtspark

  println(implicitly(content))

}
